﻿#include "delay.h"

Delay::Delay(QObject *parent)
    : Operation{parent}
{}

void Delay::run(){
    qDebug() << "Delay is Running!";
    qDebug() << QString("duration = %1 miliseconds").arg(m_nDuration);
}

int Delay::nDuration() const
{
    return m_nDuration;
}

void Delay::setNDuration(int newNDuration)
{
    m_nDuration = newNDuration;
}

void Delay::writeXML(QXmlStreamWriter& writer){
    // 写入当前操作的信息
    writer.writeStartElement("Delay");
    writer.writeAttribute("device", m_strDeviceID);
    writer.writeTextElement("duration", QString::number(m_nDuration));
    writer.writeEndElement();  // 结束当前操作的元素
}

void Delay::readFromXML(QXmlStreamReader& reader){
    QXmlStreamAttributes attributes = reader.attributes();
    if(attributes.hasAttribute("device")) {
        m_strDeviceID = attributes.value("device").toString();
    }

    QXmlStreamReader::TokenType token = reader.readNext();
    while (!(reader.tokenType() == QXmlStreamReader::EndElement &&
             reader.name() == QStringLiteral("Delay"))) {

        if (reader.name() == QStringLiteral("duration")) {
            m_nDuration = reader.readElementText().toInt();
        }

        reader.readNext();
    }
}
